package com.example.community.controller;

import com.example.community.entity.Comment;
import com.example.community.entity.DiscussPost;
import com.example.community.entity.Event;
import com.example.community.event.EventProducer;
import com.example.community.service.CommentService;
import com.example.community.service.DiscussPostService;
import com.example.community.util.CommunityConstant;
import com.example.community.util.HostHolder;
import com.example.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

@Controller
@RequestMapping("/comment")     // 评论
public class CommentController implements CommunityConstant {

    @Autowired
    private CommentService commentService;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private EventProducer eventProducer;
    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private RedisTemplate redisTemplate;

    // 添加评论、回复
    @RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
    public String addComment(@PathVariable("discussPostId") int discussPostId, Comment comment){
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        // 触发评论事件--系统通知
        Event event = new Event();
        event.setTopic(TOPIC_COMMENT);  // 主题为：评论
        event.setUserId(hostHolder.getUser().getId());  // 当前用户
        event.setEntityType(comment.getEntityType());   // 实体类型
        event.setEntityId(comment.getEntityId());       // 实体id
        event.setData("postId", discussPostId);  // 评论id -- 链接到被评论的地方

        // 获取要评论的实体的作者 -- 实体可能是帖子，也可能是评论
        if(comment.getEntityType() == ENTITY_TYPE_POST){    // 如果当前用户评论的是帖子
            DiscussPost target = discussPostService.findDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());           // 当前实体的用户id
        }else if(comment.getEntityType() == ENTITY_TYPE_COMMENT) { // 当前用户评论的是评论
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());          // 当前实体的用户id
        }
        // 开始生产 -- 发布消息
        eventProducer.fireEvent(event);

        // 对帖子评论才触发发帖事件，以及对帖子进行算分
        if(comment.getEntityType() == ENTITY_TYPE_POST){
            // 触发发帖事件
            event = new Event();
            event.setTopic(TOPIC_PUBLISH);
            event.setUserId(comment.getUserId());
            event.setEntityType(ENTITY_TYPE_POST);
            event.setEntityId(discussPostId);
            eventProducer.fireEvent(event);

            // 计算帖子分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussPostId);

        }


        return "redirect:/discuss/detail/" + discussPostId;


    }

}
